<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
use proxy\HttpProxy;
use helper\SessionHelper;
use helper\MenuHelper;

class LoginController extends BaseController
{
    public function actionIndex()
    {
         $admin_id = $this->session->get('admin_user_id');
         if (!empty($admin_id)) {
             $this->myRedirect("/index");
         } else {
             return $this->myRenderFile('@tpl/page/login.tpl', $this->return_page_values);
         }
    }
    
    
    //登录请求
    public function actionSubmit(){
    	$request = Yii::$app->request;
        
        $user_name = $request->post('user_name');
        $pwd = $request->post('pwd');


        $this->remote_params['user_name'] = $user_name;
        $this->remote_params['pwd'] = $pwd;

        try{
            $ret = $this->proxy->execute('admin', 'login', $this->remote_params);
            $this->session->set('admin_user_id',$ret['admin_id']);
            $this->session->set('admin_user_name',$ret['admin_name']);
            $this->session->set('vender_id',$ret['vender_id']);
            $this->session->set("menus", MenuHelper::getAssignedMenu($ret['admin_id']));

            //header('Location:'.Yii::$app->params['HOST'].'/index');

            // echo 'hehehe' ; exit ; 

            return $this->myRedirect('/index');
        }catch(\Exception $e){
            // var_dump($e) ; exit ; 
            //header('Location:'.Yii::$app->params['HOST'].'/login');
            // echo 'hahaha' ; exit ; 
            return $this->myRedirect('/login');
        }

    }

 	//登录请求
/*
    public function actionLogout(){
    	$request = Yii::$app->request;
        $this->session->set('admin_user_id',null);
        header('Location:'.Yii::$app->params['HOST'].'/login');
      

    }
*/


}
